package cn.dvptech.remotecontrol.screenmirror.virtualDisplay;

import android.opengl.Matrix;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

public class AllocateUtis {
    private static final int SIZEOF_FLOAT = 4;
    private static final int SIZEOF_SHORT = 2;

    // 创建FloatBuffer，用于存储float数组数据
    public static FloatBuffer createFloatBuffer(float[] fArr) {
        // 分配直接内存，大小为数组长度乘以每个float的字节数
        ByteBuffer allocateDirect = ByteBuffer.allocateDirect(fArr.length * SIZEOF_FLOAT);
        allocateDirect.order(ByteOrder.nativeOrder());
        FloatBuffer asFloatBuffer = allocateDirect.asFloatBuffer();
        asFloatBuffer.put(fArr);
        asFloatBuffer.position(0);
        return asFloatBuffer;
    }

    // 创建ShortBuffer，用于存储short数组数据
    public static ShortBuffer createShortBuffer(short[] sArr) {
        // 分配直接内存，大小为数组长度乘以每个short的字节数
        ByteBuffer allocateDirect = ByteBuffer.allocateDirect(sArr.length * SIZEOF_SHORT);
        allocateDirect.order(ByteOrder.nativeOrder());
        ShortBuffer asShortBuffer = allocateDirect.asShortBuffer();
        asShortBuffer.put(sArr);
        asShortBuffer.position(0);
        return asShortBuffer;
    }

    // 创建一个单位矩阵
    public static float[] matrix() {
        float[] fArr = new float[16];
        Matrix.setIdentityM(fArr, 0);
        return fArr;
    }
}
